package com.newcoin.chat.controller;

import com.newcoin.chat.beans.entity.FriendApplicationEntity;
import com.newcoin.chat.beans.vo.*;
import com.newcoin.chat.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/friend")
public class FriendController {
    @Autowired
    private FriendService service;

    /**
     * 根据账号后者昵称查询好友
     *
     * @param parameter
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/getFriendInfo")
    public ResultVo<List<UserVo>> getFriendInfoByFriendNicknameOrAccount(@RequestParam String parameter) {
        try {
            return service.getFriendInfoByFriendNicknameOrAccount(parameter);
        } catch (Exception e) {
            e.printStackTrace();
            ResultVo<List<UserVo>> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统出错");
            return resultVo;
        }
    }

    /**
     * 新增添加好友申请
     *
     * @param userId
     * @param friendId
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/addFriendApplicationInfo")
    public ResultVo<String> addFriendApplicationInfo(long userId, long friendId, String remark) {
        try {
            return service.addFriendApplicationInfo(userId, friendId, remark);
        } catch (Exception e) {
            ResultVo<String> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统出错");
            return resultVo;
        }
    }

    /**
     * 是否有新的好友申请消息
     *
     * @param userId
     * @return
     */
    @GetMapping(value = "/isNewFriendApplication")
    public ResultVo<Boolean> isThereANewFriendApplication(@RequestParam long userId) {
        try {
            return service.isThereANewFriendApplication(userId);
        } catch (Exception e) {
            ResultVo<Boolean> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统出错");
            return resultVo;
        }
    }

    /**
     * 获取最新10条的好友申请
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/getFriendApplicationList")
    public ResultVo<List<FriendApplicationEntity>> getFriendApplicationList(@RequestParam long userId) {
        try {
            return service.getFriendApplicationList(userId);
        } catch (Exception e) {
            e.printStackTrace();
            ResultVo<List<FriendApplicationEntity>> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统出错");
            return resultVo;
        }
    }

    /**
     * 通过或拒绝朋友申请
     *
     * @param friendApplicationId
     * @param state
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/passOrRejectFriendApplication")
    public ResultVo<String> passOrRejectFriendApplication(long friendApplicationId, int state) {
        try {
            return service.passOrRejectFriendApplication(friendApplicationId, state);
        } catch (Exception e) {
            ResultVo<String> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统出错");
            return resultVo;
        }
    }

    /**
     * 删除好友
     *
     * @param userId
     * @param friendId
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/deleteFriend")
    public ResultVo<String> deleteFriend(long userId, long friendId) {
        try {
            return service.deleteFriend(userId, friendId);
        } catch (Exception e) {
            ResultVo<String> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统出错");
            return resultVo;
        }
    }

    /**
     * 打开或关闭阅后即焚
     *
     * @param userId
     * @param friendId
     * @param state
     * @return
     */
    @PostMapping(value = "/openOrCloseAfterReadInc")
    public ResultVo<String> openOrCloseAfterReadingIncineration(long userId, long friendId, int state) {
        try {
            return service.openOrCloseAfterReadingIncineration(userId, friendId, state);
        } catch (Exception e) {
            ResultVo<String> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统出错");
            return resultVo;
        }
    }

    /**
     * 获取所有的好友申请信息
     *
     * @param userId
     * @param page
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/getAllFriendApplication")
    public ResultVo<FriendApplicationVo> getAllFriendApplicationMessages(@RequestParam long userId, @RequestParam int page) {
        try {
            return service.getAllFriendApplicationMessages(userId, page);
        } catch (Exception e) {
            ResultVo<FriendApplicationVo> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统出错");
            return resultVo;
        }
    }

    /**
     * 获取通讯录列表
     *
     * @param userId
     * @return
     */
    @GetMapping(value = "/getContactList")
    public ResultVo<Map<String, Object>> getContactList(long userId) {
        try {
            return service.getContactList(userId);
        } catch (Exception e) {
            ResultVo<Map<String, Object>> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统出错");
            return resultVo;
        }
    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping(value = "/getUserInfo")
    public ResultVo<UserVo> getUserInfo(Long userId) {
        try {
            return service.getUserInfo(userId);
        } catch (Exception e) {
            e.printStackTrace();
            ResultVo<UserVo> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统出错");
            return resultVo;
        }
    }

    /**
     * 获取与朋友的阅后即焚状态
     *
     * @param userId
     * @param friendId
     * @return
     */
    @GetMapping(value = "/getAfterReadingIncinerationState")
    public ResultVo<Integer> getAfterReadingIncinerationState(long userId, long friendId) {
        try {
            return service.getAfterReadingIncinerationState(userId, friendId);
        } catch (Exception e) {
            e.printStackTrace();
            ResultVo<Integer> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统出错");
            return resultVo;
        }
    }

    /**
     * 获取离线消息
     *
     * @param userId
     * @param friendId
     * @return
     */
    @GetMapping(value = "/getOfflineMessage")
    public ResultVo<List<FriendOfflineMsgVo>> getOfflineMessage(Long userId, Long friendId) {
        try {
            return service.getOfflineMessage(userId, friendId);
        } catch (Exception e) {
            e.printStackTrace();
            ResultVo<List<FriendOfflineMsgVo>> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统出错");
            return resultVo;
        }
    }
}
